package dm.ass2.io;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.util.Arrays;

import org.apache.hadoop.io.Writable;

public class CodebookUsersWritable implements Writable {
	private int userClusterId;
	private int[] userIds;
	private float[] itemsClustersRating;
	
	public CodebookUsersWritable(){
		super();
	}
	public CodebookUsersWritable(int userClusterId, int[] userIds, float[] clusterItemsRating) {
		super();
		this.userClusterId = userClusterId;
		this.userIds = userIds;
		this.itemsClustersRating = clusterItemsRating;
	}

	@Override
	public void readFields(DataInput in) throws IOException {
		this.userClusterId = in.readInt();
		
		// Reading the user ids in this user cluster.
		int numOfUsersInCluster = in.readInt();
		this.userIds = new int[numOfUsersInCluster];
		for (int i = 0; i < this.userIds.length; i++) {
			this.userIds[i] = in.readInt();
		}

		// Reading the rating of item clusters.
		int numberOfItemsCluster = in.readInt();
		this.itemsClustersRating = new float[numberOfItemsCluster];
		for (int i = 0; i < this.itemsClustersRating.length; i++) {
			this.itemsClustersRating[i] = in.readFloat();
		}
	}

	@Override
	public void write(DataOutput out) throws IOException {
		out.writeInt(this.userClusterId);
		out.writeInt(this.userIds.length);
		for (int i = 0; i < this.userIds.length; i++) {
			out.writeInt(this.userIds[i]);
		}
		
		out.writeInt(this.itemsClustersRating.length);
		for (int i = 0; i < this.itemsClustersRating.length; i++) {
			out.writeFloat(this.itemsClustersRating[i]);
		}
	}
	public int getUserClusterId() {
		return userClusterId;
	}

	public int[] getUsrIds() {
		return userIds;
	}

	public float[] getItemClusterRating() {
		return itemsClustersRating;
	}

	@Override
	public String toString() {
		StringBuilder out = new StringBuilder();
		out.
		 append(this.userClusterId).
		 append(" ").
		 append(Arrays.toString(this.userIds)).
		 append(" ").
		 append(Arrays.toString(this.itemsClustersRating));

		return out.toString();
	}

}
